package org.javagril.yuetong.service;

import org.javagril.yuetong.bean.RespPageBean;
import org.javagril.yuetong.bean.Truck;
import org.javagril.yuetong.bean.TruckTeam;
import org.javagril.yuetong.mapper.TruckMapper;
import org.javagril.yuetong.mapper.TruckTeamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TruckTeamService {
    @Autowired
    TruckTeamMapper truckTeamMapper;
    @Autowired
    TruckMapper truckMapper;
    public List<TruckTeam> getAllTruckTeam() {
        return truckTeamMapper.getAllTruckTeam();
    }


    public RespPageBean getAllTruckTeam(Integer page, Integer size) {
        if (page != null && size != null) {
            page = (page - 1) * size;
        }
        List<TruckTeam> list = truckTeamMapper.getAllTruckTeamByPage(page, size);
        Long total = truckTeamMapper.getTotal();
        RespPageBean respPageBean = new RespPageBean();
        respPageBean.setData(list);
        respPageBean.setTotal(total);
        return respPageBean;
    }

    public List<Truck> getTruckTeamByTruckTeamId(Integer id) {

        return truckMapper.getTruckTeamByTruckTeamId(id);
    }


    /*添加车队*/
    public Boolean addTruckTeam(TruckTeam truckTeam) {
        truckTeam.setAltertime(new Date());
        truckTeam.setCheckintime(new Date());
        truckTeam.setIsdelete(-1);
        truckTeamMapper.insertSelective(truckTeam);
        Integer teamid = truckTeam.getTeamid();
        return truckMapper.updateTruckByTruckTeamId(teamid, truckTeam.getTids()) == truckTeam.getTids().length;
    }

    public boolean updateTruckTeam(TruckTeam truckTeam) {
        truckTeamMapper.updateByPrimaryKeySelective(truckTeam);
        truckMapper.updateTruckByTtId(truckTeam.getTeamid());
        return truckMapper.updateTruckByTruckTeamId(truckTeam.getTeamid(), truckTeam.getTids()) == truckTeam.getTids().length;
    }

    /*车队删除操作*/
    public Integer deleteTruckTeam(Integer id) {
        TruckTeam tkt= new TruckTeam();
        tkt.setIsdelete(0);
        tkt.setTeamid(id);
        /*删除属于更新信息操作 故返回 update*/
        return truckTeamMapper.updateByPrimaryKeySelective(tkt);
    }
}
